﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CommonServices;
using CK.Plugin;
using System.Resources;
using System.Globalization;

namespace AutoClick
{
    /// <summary>
    /// Panel for the AutoClick's editor
    /// </summary>
    public partial class AutoClickUserEditor : UserControl
    {
        #region Variables
        private AutoClickEditor _editor;
        private ResourceManager _resourcesForText;
        #endregion

        #region Constructor and panel load
        public AutoClickUserEditor(AutoClickEditor e)
        {
            InitializeComponent();
            _editor = e;

            //select language of user's OS
            if (CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "fr")
                _resourcesForText = new ResourceManager(typeof(TextFr));
            else
                _resourcesForText = new ResourceManager(typeof(TextEn));
            SetLanguage();

            //Initialize attributes
            _radiusValue.Value = _editor.AreaOfReferenceClickRadius;
            _askedShowGauge.Checked = _editor.ShowGauge;
            _askedShowTypesOfClick.Checked = _editor.ShowTypesOfClickMenu;
            _askedShowCircle.Checked = _editor.ShowAreaOfReferenceClick;
            _commonTimerMultiple.Value = _editor.CommonTimerMultiple;
        }
        private void AutoClickUserEditor_Load(object sender, EventArgs e) { }
        #endregion

        #region Change parameters
        /// <summary>
        /// Updates the value of radius
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _radiusValue_ValueChanged(object sender, EventArgs e)
        {
            _editor.AreaOfReferenceClickRadius = (int)_radiusValue.Value;
        }
        /// <summary>
        /// Updates new algorithm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _algorithmList_SelectedIndexChanged(object sender, EventArgs e)
        {
            _editor.Algorithm = GetSelectedAlgorithm();
        }
        /// <summary>
        /// Updates visibility of the menu type of click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _askedShowTypesOfClick_CheckedChanged(object sender, EventArgs e)
        {
            _editor.ShowTypesOfClickMenu = (bool)_askedShowTypesOfClick.Checked;
        }
        /// <summary>
        /// Updates the visibility of gauge
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _askedShowGauge_CheckedChanged(object sender, EventArgs e)
        {
            _editor.ShowGauge = (bool)_askedShowGauge.Checked;
        }
        /// <summary>
        /// Updates the visibility of the circle of acceptation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _askedShowCircle_CheckedChanged(object sender, EventArgs e)
        {
            _editor.ShowAreaOfReferenceClick = (bool)_askedShowCircle.Checked;
        }
        /// <summary>
        /// Updates the value of CommonTimer's multiple
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _commonTimerMultiple_ValueChanged(object sender, EventArgs e)
        {
            _editor.CommonTimerMultiple = (int)_commonTimerMultiple.Value;
            _labelDelay.Text = (String)_resourcesForText.GetObject("_labelDelay") + _editor.CommonTimerMultiple * _editor.CommonTimerInterval + " ms";
        }
        /// <summary>
        /// Updates the default type of click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _typesOfClickList_SelectedIndexChanged(object sender, EventArgs e)
        {
            _editor.TypeOfClick = GetSelectedTypeOfClicks();
        }
        #endregion

        #region Translations
        /// <summary>
        /// Method that returns the selected algorithm
        /// </summary>
        /// <returns></returns>
        public string GetSelectedAlgorithm()
        {
            if ((string)_algorithmList.SelectedItem == (string)_resourcesForText.GetObject("ClickIfPointerIsInRadius"))
                return "ClickIfPointerIsInRadius";
            if ((string)_algorithmList.SelectedItem == (string)_resourcesForText.GetObject("ClickIfPointerStayInRadius"))
                return "ClickIfPointerStayInRadius";
            if ((string)_algorithmList.SelectedItem == (string)_resourcesForText.GetObject("PercentageCalculating"))
                return "PercentageCalculating";
            else return "ClickIfPointerIsInRadius";
        }
        /// <summary>
        /// Method that returns the selected default type of click
        /// </summary>
        /// <returns></returns>
        public string GetSelectedTypeOfClicks()
        {
            if ((string)_typesOfClickList.SelectedItem == (string)_resourcesForText.GetObject("clickLeft"))
                return "clickLeft";
            if ((string)_typesOfClickList.SelectedItem == (string)_resourcesForText.GetObject("clickRight"))
                return "clickRight";
            if ((string)_typesOfClickList.SelectedItem == (string)_resourcesForText.GetObject("clickDoubled"))
                return "clickDoubled";
            if ((string)_typesOfClickList.SelectedItem == (string)_resourcesForText.GetObject("clickHold"))
                return "clickHold";
            else return "noClick";
        }
        /// <summary>
        /// Set the text on panel according to the language of user's OS
        /// </summary>
        public void SetLanguage()
        {
            _labelAlgorithm.Text = (String)_resourcesForText.GetObject("_labelAlgorithm");
            _labelChooseClick.Text = (String)_resourcesForText.GetObject("_labelChooseClick");
            _labelMultipleCommonTimer.Text = (String)_resourcesForText.GetObject("_labelMultipleCommonTimer");
            _labelRadius.Text = (String)_resourcesForText.GetObject("_labelRadius");
            _labelDelay.Text = (String)_resourcesForText.GetObject("_labelDelay") + _editor.CommonTimerMultiple*_editor.CommonTimerInterval+" ms";
            _labelPixel.Text = (String)_resourcesForText.GetObject("_labelPixel");
            
            _askedShowCircle.Text = (String)_resourcesForText.GetObject("_askedShowCircle");
            _askedShowGauge.Text = (String)_resourcesForText.GetObject("_askedShowGauge");
            _askedShowTypesOfClick.Text = (String)_resourcesForText.GetObject("_askedShowTypesOfClick");

            _tabPage1.Text = (String)_resourcesForText.GetObject("_tabPage1");
            _tabPage2.Text = (String)_resourcesForText.GetObject("_tabPage2");
            
            _algorithmList.Items.Add((String)_resourcesForText.GetObject("ClickIfPointerStayInRadius"));
            _algorithmList.Items.Add((String)_resourcesForText.GetObject("ClickIfPointerIsInRadius"));
            _algorithmList.Items.Add((String)_resourcesForText.GetObject("PercentageCalculating"));
            _typesOfClickList.Items.Add((String)_resourcesForText.GetObject("clickLeft"));
            _typesOfClickList.Items.Add((String)_resourcesForText.GetObject("clickRight"));
            _typesOfClickList.Items.Add((String)_resourcesForText.GetObject("clickDoubled"));
            _typesOfClickList.Items.Add((String)_resourcesForText.GetObject("clickHold"));
            _typesOfClickList.Items.Add((String)_resourcesForText.GetObject("noClick"));
            
            _algorithmList.SelectedItem = _resourcesForText.GetObject(_editor.Algorithm);
            _typesOfClickList.SelectedItem = _resourcesForText.GetObject(_editor.TypeOfClick);
        }
        #endregion
    }
}
